import React, {useState, useEffect} from "react";
import { Modal, Table} from "antd";



const NextNodeHandler = (props) => {
  const {
    handleClose,
    drawData,
    handleChangeProperties,
    selectedModel,
  } = props;

  const [nodeConfigVisible, setNodeConfigVisible] = useState(true);
  const [selectedKeys, setSelectedKeys] = useState([]);
  const [dataSource, setDataSource] = useState([]);

  useEffect(() => {
    if (!nodeConfigVisible) {
      setTimeout(() => {
        handleClose();
      }, 250);
    }
  }, [nodeConfigVisible]);

  useEffect(() => {
    const {controlNode=[]} = selectedModel?.model?.properties ?? {};

    setSelectedKeys(controlNode);


  }, [selectedModel]);


  useEffect(() => {

    const {nodes = []} = drawData ?? {};
    const tmpList = [];
    nodes.map(item => {

      const {forcePick = '0'} = item?.model?.properties ?? {};
      if (forcePick === '1'){
        tmpList.push(item);
      }

      return item;
    })
    setDataSource(tmpList);
  }, [drawData]);



  const handleOk = () => {
    handleChangeProperties(
      'controlNode',
      selectedKeys,
      selectedModel.model,
      selectedModel.shape,
      false,
    )

    setNodeConfigVisible(false);
  }


  const columns = [
    {
      title: '节点编码',
      dataIndex: 'overrideid',
      key: 'overrideid',
      render: (_, record) => <div>{record?.model?.properties?.overrideid ?? ''}</div>,
    },
    {
      title: '节点名称',
      dataIndex: 'age',
      key: 'age',
      render: (_, record) => <div>{record?.model?.properties?.name ?? ''}</div>,
    },
  ];


  const rowSelection = {
    onChange: (selectedRowKeys) => {
      setSelectedKeys(selectedRowKeys);
    },
    selectedRowKeys: selectedKeys,

  };

  return (
    <Modal
      visible={nodeConfigVisible}
      onCancel={() => setNodeConfigVisible(false)}
      onOk={handleOk}
      title='配置指定流程节点处理人'
    >
      <Table
        rowSelection={rowSelection}
        dataSource={dataSource}
        columns={columns}
        rowKey={record => record?.model?.properties?.overrideid ?? ''}
      />
    </Modal>
  )
}

export default NextNodeHandler;
